#ifndef MESH_FACTORY
#define MESH_FACTORY

#include "Export.h"
#include "ILoader.h"
#include "Mesh.h"
#include <iostream>
#include <string>
#include <map>

namespace glow
{
	
  class SHARED MeshFactory
  {
  public :
	/**
	 * Add a link between a file extension and a specific Loader
	 **/
    static void registerMeshLoader(std::string ext, ILoader<MeshData> *loader)
    {
      if(loader)
	TheMap[ext] = loader;
    }
		
	/**
	 * Create the Mesh
	 * \param filename is the path to the file to load
	 **/  
    static Mesh *create(std::string filename, bool useVBO = true, GLenum updateType = STREAM)
    {
      size_t ext_pos = filename.find_last_of('.');
      Mesh *mesh = NULL;
			
      if( ext_pos != string::npos )
	{
	  std::string ext1 = filename.substr( ext_pos );
	  map<std::string, ILoader<MeshData> *>::iterator found = TheMap.find(ext1);
	  if(found != TheMap.end())
	    {
	      MeshData *data = found->second->load(filename);
	      return new Mesh(data, useVBO, updateType);
	    }
	  else
	    std::cout << "Extension not supported" << std::endl;
	}
      else
	std::cout<<"404 : Extension not Found"<<std::endl;
      return mesh;
    }
		
  private :
	
    static std::map<std::string, ILoader<MeshData> *> TheMap;
  };

  std::map<std::string,ILoader<MeshData> *> MeshFactory::TheMap;

}

#endif
